package com.itheima.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.config.AdminHolder;
import com.itheima.domain.db.Admin;
import com.itheima.manager.WebManager;
import com.itheima.util.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.Duration;
import java.util.Map;

@RestController
public class WebController {
    @Autowired
    WebManager webManager;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    //生成验证码
    @GetMapping("/web/users/verification")
    public void verification(String uuid, HttpServletResponse response) throws IOException {
        //1.获取uuid值
        //2.调用manager生成验证码图片
        LineCaptcha lineCaptcha = webManager.verification(uuid);

        //3.使用response对象将图片写回去
        lineCaptcha.write(response.getOutputStream());
    }

    //用户登录
    @PostMapping("/web/users/login")
    public ResponseEntity login(@RequestBody Map<String, String> map) {
        //1.接受4个参数
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");
//        System.out.println(verificationCode);
        //2.调用manager完成登陆操作
        return webManager.login(username, password, verificationCode, uuid);
    }

    //进行token的验证
    @GetMapping("/user/verify")
    public String verify(@RequestHeader("Authorization") String token) {
        System.out.println(token);
        //不为空时，要进行token的续期
        try {
            String value = stringRedisTemplate.opsForValue().get(ConstantUtil.ADMIN_TOKEN + token);
            if (StrUtil.isNotBlank(value)) {
                Admin admin = JSON.parseObject(value, Admin.class);
                if (admin != null) {
                    AdminHolder.set(admin);
                    //续期
                    stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN + token, value, Duration.ofHours(1));
                }
            }else {
                //token为空，跳转到login.html
                return "401";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "401";
        }
        return "ok";
    }

    @GetMapping("/user/tokens")
    public String hasToken(@RequestHeader("Authorization") String token) {
        System.out.println(token);
        //不为空时，要进行token的续期
        try {
            String value = stringRedisTemplate.opsForValue().get(ConstantUtil.ADMIN_TOKEN + token);
            if (StrUtil.isNotBlank(value)) {
                Admin admin = JSON.parseObject(value, Admin.class);
                if (admin != null) {
                    AdminHolder.set(admin);
                    //续期
                    stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN + token, value, Duration.ofHours(1));
                }
            }else {
                //token为空，跳转到login.html
                return "401";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "401";
        }
        return "ok";
    }


}

